package com.woniuxy.dao.impl;

import com.woniuxy.dao.UserDao;
import com.woniuxy.entity.User;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;

/**
 * 学生数据操作的实现类，使用文件实现
 */
public class UserDaoDbImpl implements UserDao {
    @Override
    public User getUserByUname(String uname) {
        try {
            User userFind = null;
            String className = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql:///db2";
            String user = "root";
            String password="123";
            Class.forName(className);
            Connection connection = DriverManager.getConnection(url, user, password);
            String sql="select * from user where uname=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, uname);
            ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()){
                userFind=new User();
                userFind.setUid(resultSet.getString("uid"));
                userFind.setUname(resultSet.getString("uname"));
                userFind.setPassword(resultSet.getString("password"));
                userFind.setEmail(resultSet.getString("email"));
                userFind.setAge(Integer.valueOf(resultSet.getString("age")));
            }
            statement.close();
            connection.close();
            return userFind;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addUser(User adduser) {
        try {
            User userFind = null;
            String className = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql:///db2";
            String user = "root";
            String password="123";
            Class.forName(className);
            Connection connection = DriverManager.getConnection(url, user, password);
            String sql="insert into user values(?,?,?,?,?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, adduser.getUid());
            statement.setString(2, adduser.getUname());
            statement.setString(3, adduser.getPassword());
            statement.setInt(4, adduser.getAge());
            statement.setString(5, adduser.getEmail());
            statement.executeUpdate();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getMaxUid() {
        try {
            String maxUid="0";
            BufferedReader br=new BufferedReader(new FileReader("d:\\user.txt"));
            String line=null;
            while ((line=br.readLine())!=null){
                String[] strings = line.split(",");
                maxUid=strings[0];
            }
            return maxUid;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
